package daoImp;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;
import model.Admin;
import model.User;
import dao.UserDao;

public class UserDaoImp implements UserDao {

	Session session = null;

	@Override
	public void addUser(User user) {
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.save(user);// 添加数据
			System.out.println(user.getUname() + " Added");
			session.getTransaction().commit();// 提交事务
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null)
				session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}

	}

	@Override
	public void deleteUser(int id) {
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			User user = (User) session.load(User.class, id);
			session.delete(user);
			session.getTransaction().commit();// 提交
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null)
				session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}

	}

	@Override
	public User loadUser(int uid) {
		User user = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			user = (User) session.get(User.class, uid);
			session.getTransaction().commit();// 提交
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null)
				session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
		return user;
	}

	@Override
	public void update(User user) {
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.update(user);
			// session.save(user);
			session.getTransaction().commit();// 提交
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null)
				session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}

	}

	
	@Override
	public List<User> list() {
		List<User> users = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			System.out.println("Query User");
			users = session.createQuery("from User").list();// 查询全部
			User u=new User("admin", "admin", true);
			if(users.contains(u)){
				users.remove(u);
				System.out.println("aaa");
			}
			session.getTransaction().commit();// 提交
		} catch (Exception e) {
			e.printStackTrace();
			if (session != null)
				session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
		return users;
	}

	@Override
	public User login(String uname, String uPwd) {
		User u = null;
		Object o=null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			o= session.createQuery("from User where uname=?")
					.setParameter(0, uname)
					.uniqueResult();
			if(o==null){
				return u;
			}
			if(((User) o).getUname().equals(uname)&&((User) o).getUpassword().equals(uPwd)){
				u=(User) o;
			}
		} finally {
			HibernateUtil.close(session);
		}
		return u;
	}

	@Override
	public boolean isExist(String uname) {
		Object o=null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			o= session.createQuery("from User where uname=?")
					.setParameter(0, uname)
					.uniqueResult();
			System.out.println(o);
			if(o!=null){
				return true;
			}

		} finally {
			HibernateUtil.close(session);
		}
		return false;
	}

	@Override
	public User loadUser(String uname) {
		User u = null;
		Object o=null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			o= session.createQuery("from User where uname=?")
					.setParameter(0, uname)
					.uniqueResult();
			System.out.println(o);
			if(o==null){
				return u;
			}
			if(((User) o).getUname().equals(uname)){
				u=(User) o;
			}
			
		} finally {
			session.close();
		}
		return u;
	}

}
